package com.xyz.app.impl;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.google.gson.reflect.TypeToken;
import com.xyz.app.inter.HtmlParser;

public class XVHtmlParser implements HtmlParser {

    private String host;

    public XVHtmlParser(String host) {
        this.host = host;
    }

    @Override
    public List<Map<String, String>> parseList(String rUrl, String content) {
        String ip = null;
        try {
            ip = new URL(rUrl).getHost();
        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        if (rUrl.contains("/video")) {
            if (rUrl.contains("/endDetail")) {
                return new ArrayList<Map<String, String>>();
            }
            return parseRelated(ip, content);
        }
        Document doc = Jsoup.parse(content);
        final List<Map<String, String>> mediaInfos = new ArrayList<Map<String, String>>(50);
        Elements elements = doc.select("#content").select(".thumb-block");
        for (Element ele : elements) {
            try {
                Map<String, String> media = new HashMap<String, String>();
                Element e = ele.select("a").get(1);
                media.put("videoUri", e.attr("href"));
                media.put("link", "https://" + ip + e.attr("href"));
                media.put("relatedUrl", "https://" + host + e.attr("href"));
                media.put("linkHost", host);
                media.put("title", e.attr("title").replaceAll("([a-z0-9A-Z]*)\\.com", ""));
                String preview = ele.select("img").get(0).attr("data-src");
                media.put("preview", preview);
                media.put("duration", ele.select(".duration").get(0).html());
                mediaInfos.add(media);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return mediaInfos;
    }

    private List<Map<String, String>> parseRelated(String ip, String content) {
        String[] cts = content.split("video_related=");
        if (cts.length == 2) {
            cts = cts[1].split(";window.wpn_categories");
            if (cts.length == 2) {
                List<Map<String, String>> vlist = JsonUtil.json2object(cts[0], new TypeToken<List<Map<String, String>>>() {
                });
                if (vlist != null) {
                    for (Map<String, String> media : vlist) {
                        media.put("videoUri", media.get("u"));
                        media.put("link", "https://" + ip + media.get("u"));
                        media.put("relatedUrl", "https://" + host + media.remove("u"));
                        media.put("linkHost", host);
                        media.put("title", media.remove("t").replaceAll("([a-z0-9A-Z]*)\\.com", ""));
                        media.put("preview", media.remove("i"));
                        media.put("duration", media.remove("d"));
                    }
                    return vlist;
                }
            }
        }
        return new ArrayList<Map<String, String>>();
    }

    @Override
    public Map<String, String> parseDetail(String rUrl, String content) {
        Map<String, String> media = new HashMap<String, String>();
        Document doc = Jsoup.parse(content);
        try {
            String videoUrl = doc.select("#html5video").select("a").get(0).attr("href");
            media.put("videoUrl", videoUrl);
            media.put("downloadUrl", videoUrl);
            String[] tokens = rUrl.split("/");
            media.put("downloadTitle", tokens[tokens.length - 1] + ".mp4");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return media;
    }

    @Override
    public String getUrl(String url, int page) {
        // 最新视频
        if (url.contains("/new")) {
            if (page == 0) {
                return url.replace("/new", "");
            } else {
                return url + page;
            }
        }
        if (url.contains("/best")) {
            if (page > 0) {
                return url + page;
            }
        }
        if (url.contains("/porn")) {
            if (page > 0) {
                return url + page;
            }
        }
        if (url.contains("/video")) {
            if (page == 0) {
                return url;
            } else {
                return url + "/endDetail";
            }
        }
        return url + "&p=" + page;
    }
}
